package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShoppingCartImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    //添加购物车
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断购物车是否是第一次添加
        List<ShoppingCart> list = shoppingCartMapper.ifExistShoppingCart(shoppingCart);

        //如果不是第一次则将购物车表的数量字段加一即可
        if (list!=null && list.size()>0){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumber(cart);
        }

        //如果是第一次则新增购物车
        else {
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId!=null){
                Dish dish = dishMapper.checkDishById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
            }
            else {
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.setmealById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setNumber(1);
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
            }

            shoppingCartMapper.addShoppingCart(shoppingCart);
        }
    }

    //查看购物车
    @Override
    public List<ShoppingCart> checkShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.checkShoppingCart(shoppingCart);
        return shoppingCarts;
    }

    //清空购物车
    @Override
    public void cleanShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.cleanShoppingCart(shoppingCart);
    }

    //删除购物车中的一个商品
    @Override
    public void deleteByOneShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //判断该商品的份数
        List<ShoppingCart> list = shoppingCartMapper.ifExistShoppingCart(shoppingCart);
        ShoppingCart cart = list.get(0);
        Integer number = cart.getNumber();
        //如果份数大于一份则对份数减一即可
        if (number>1){
            cart.setNumber(number-1);
            shoppingCartMapper.updateNumber(cart);
        }

        //如果份数为一则删除该记录
        else {
            shoppingCartMapper.deleteByOneShoppingCart(shoppingCart);
        }
    }
}
